﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class NoteDataEditManager : MonoBehaviour
    {
        public static NoteDataEditManager instance;
        public NoteDataEditorCtrl ctl;
        public bool isEditing;

        public void ShowNoteEditWindow(List<Note> ns)
        {
            if (isEditing == true) CloseWindow();
            foreach (var item in ns)
            {
                item.isEditing = true;
            }
            ctl.gameObject.active = true;
            ctl.window.SetWindowToFront();
            isEditing = true;
            ctl.selectNotes = ns;
            ctl.multiSelect = true;
            ctl.Init();
        }

        public void ShowNoteEditWindow(Note n)
        {
            if (isEditing == true)
            {
                if (n == ctl.selectNote)
                {
                    ctl.SetPosX();
                    return;
                }
                else CloseWindow();
            }
            n.isEditing = true;
            ctl.gameObject.active = true;
            ctl.window.SetWindowToFront();
            isEditing = true;
            ctl.selectNote = n;
            ctl.multiSelect = false;
            ctl.Init();
        }

        public void CloseWindow()
        {

            if (ctl.multiSelect)
            {
                foreach (var item in ctl.selectNotes)
                {
                    item.isEditing = false;
                    item.thisRenderer.color = Color.white;
                }
                for (int i = 0; i < ctl.selectNotes.Count; i++)
                {
                    ctl.selectNotes[i].thisRenderer.color = Color.white;
                    ctl.selectNotes[i].isEditing = false;
                    ctl.selectNotes.Remove(ctl.selectNotes[i]);
                }
                ctl.selectNotes = new List<Note>();
            }
            else
            {
                ctl.selectNote.thisRenderer.color = Color.white;
                ctl.selectNote.isEditing = false;
                ctl.selectNote = null;
            }
            ctl.gameObject.active = false;
            isEditing = false;
        }

        void Awake()
        {
            instance = this;
            ctl.gameObject.active = false;
            isEditing = false;
        }

        void Update()
        {

        }
    }
}